package com.ttloner.admin.interceptor;

import com.ttloner.admin.bean.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author TTLoner
 * @date 2021/7/20 20:29
 * @description 拦截器，登录检查
 */
public class LoginInterceptor implements HandlerInterceptor {
    //目标方法执行完成之前的操作
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser==null) {
            //拦截
            request.setAttribute("msg","请登录");
            request.getRequestDispatcher("/login").forward(request,response);
            return false;
        }
        //放行
        return true;
    }
}
